//
//  YSLSpecialViewController.m
//  used
//
//  Created by lanou on 16/7/15.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "YSLSpecialViewController.h"

@interface YSLSpecialViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *imageArray;
@property (nonatomic, strong)NSMutableArray *webArray;
@end

@implementation YSLSpecialViewController

- (NSMutableArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (NSMutableArray *)webArray
{
    if (!_webArray) {
        _webArray = [NSMutableArray array];
    }
    return _webArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.tableView registerNib:[UINib nibWithNibName:@"YSLSpecialCell" bundle:nil] forCellReuseIdentifier:@"YSLSpecialCell_cell"];
    [self setupRefresh];
    [self loadData];
    
   
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction)];
    swip.direction = UISwipeGestureRecognizerDirectionRight;
    [self.tableView addGestureRecognizer:swip];

}

- (void)swipAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 加载数据
- (void)loadData
{

//    [SVProgressHUD show];
    //数据请求
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"typeId"] = @0;
    params[@"pageIndex"] = @1;
    [RequestManager fetchDataWithUrl:@"http://api.91sph.com/specialTopic/advList" parameter:params method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//                NSLog(@"%@",bigDic);
        
        //添加数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"advList"][@"result"]) {
            
            [self.imageArray addObject:smallDic[@"picUrl"]];

            NSArray *array = [smallDic[@"originalLinkUrl"] componentsSeparatedByString:@"url="];
            
            [self.webArray addObject:array[1]];
            
        }
    } updateUI:^{

        [self.tableView reloadData];
        
        // 进入下拉刷新状态
        [self loadNewUsers];
    }];
}

#pragma mark - 添加刷新控件
- (void)setupRefresh
{
    
    //添加下拉刷新
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.imageArray removeAllObjects];
        [self loadNewUsers];
        
    }];
    
    //添加上拉加载
//    self.tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
//        
//        [self loadMoreUsers];
//    }];
    
}


#pragma mark - 刷新加载用户数据

//加载列表的数据 下拉
- (void)loadNewUsers
{
    //数据请求
    [RequestManager fetchDataWithUrl:@"http://api.91sph.com/specialTopic/advList" parameter:@{@"typeId":@0,@"pageIndex":@1} method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //        NSLog(@"%@",bigDic);
        
        //添加轮播图的数据源
        for (NSDictionary *smallDic in bigDic[@"data"][@"advList"][@"result"]) {
            
            [self.imageArray addObject:smallDic[@"picUrl"]];
         
        }
    } updateUI:^{
        
        //结束刷新
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
        
    }];
    
}



////加载用户数据 上拉
//- (void)loadMoreUsers
//{
//    //数据请求
//    [RequestManager fetchDataWithUrl:@"http://api.91sph.com/specialTopic/advList" parameter:@{@"typeId":@0,@"pageIndex":@1} method:@"POST" compeleted:^(NSData *data) {
//        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        //        NSLog(@"%@",bigDic);
//        
//        //添加轮播图的数据源
//        for (NSDictionary *smallDic in bigDic[@"data"][@"advList"][@"result"]) {
//            
//            [self.imageArray addObject:smallDic[@"picUrl"]];
//        }
//    } updateUI:^{
//        
//        //结束刷新
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
//        [self.tableView reloadData];
//        
//    }];
//    
//}


#pragma mark---tableViewDelegate----
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.webArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YSLSpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSLSpecialCell_cell" forIndexPath:indexPath];
    
    [cell.specialImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.row]]];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 215;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    YSLWebViewController *WebViewVC = [[YSLWebViewController alloc] init];
    WebViewVC.imageArray = self.webArray[indexPath.row];
//    NSLog(@"spe-------------------%@",WebViewVC.imageArray);
    [self.navigationController pushViewController:WebViewVC animated:YES];
}



@end
